package com.example.yin.controller;

import com.example.yin.common.ErrorMessage;
import com.example.yin.common.SuccessMessage;
import com.example.yin.common.aop.MyLog;
import com.example.yin.config.OSSConfiguration;
import com.example.yin.constant.Constants;
import com.example.yin.domain.SongList;
import com.example.yin.domain.UserSongList;
import com.example.yin.service.OssService;
import com.example.yin.service.impl.SongListServiceImpl;
import org.apache.commons.lang3.ObjectUtils.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class SongListController {

    @Autowired
    private SongListServiceImpl songListService;

    @Autowired
    private OssService ossService;
    @Autowired
    private OSSConfiguration ossConfiguration;


    @Configuration
    public static class MyPicConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/img/songListPic/**")
                    .addResourceLocations(Constants.SONGLIST_PIC_PATH);
        }
    }

    // 添加歌单
    @ResponseBody
    @MyLog(value = "创建歌单")
    @RequestMapping(value = "/songList/add", method = RequestMethod.POST)
    public Object addSongList(@RequestParam("title") String title,@RequestParam("userid") String userid,String introduction,String style,@RequestParam("file") MultipartFile file) {

        String picpath = ossConfiguration.getSongListPicPath()+file.getOriginalFilename();

        ossService.uploadFile(file,picpath);

        SongList songList = new SongList();
        songList.setTitle(title);
        songList.setIntroduction(introduction);
        songList.setStyle(style);
        songList.setPic("/"+picpath);
        UserSongList userSongList = new UserSongList();



        if(userid != null){
            songListService.addSongList(songList);
            Integer songlistid = songListService.searchSongListID(title);
            Integer uid = Integer.parseInt(userid);
            userSongList.setConsumer(uid);
            userSongList.setSonglist(songlistid);
            boolean resp = songListService.addUserSongList(userSongList);
            if (resp) {
                    return new SuccessMessage<Null>("添加成功").getMessage();
                } else {
                    return new ErrorMessage("添加失败").getMessage();
                }
            }
        else {
            boolean res = songListService.addSongList(songList);
            if (res) {
                return new SuccessMessage<Null>("添加成功").getMessage();
            } else {
                return new ErrorMessage("添加失败").getMessage();
            }
        }

    }


    // 删除歌单
    @RequestMapping(value = "/songList/delete", method = RequestMethod.GET)
    public Object deleteSongList(HttpServletRequest req) {
        String id = req.getParameter("id");

        boolean res = songListService.deleteSongList(Integer.parseInt(id));
        if (res) {
            return new SuccessMessage<Null>("删除成功").getMessage();
        } else {
            return new ErrorMessage("删除失败").getMessage();
        }
    }

    // 返回所有歌单
    @RequestMapping(value = "/songList", method = RequestMethod.GET)
    public Object allSongList() {
        return new SuccessMessage<List<SongList>>(null, songListService.allSongList()).getMessage();
    }

    // 返回标题包含文字的歌单
    @RequestMapping(value = "/songList/likeTitle/detail", method = RequestMethod.GET)
    public Object songListOfLikeTitle(HttpServletRequest req) {
        String title = req.getParameter("title").trim();

        return new SuccessMessage<List<SongList>>(null, songListService.likeTitle('%' + title + '%')).getMessage();
    }

    // 返回指定类型的歌单
    @RequestMapping(value = "/songList/style/detail", method = RequestMethod.GET)
    public Object songListOfStyle(HttpServletRequest req) {
        String style = req.getParameter("style").trim();

        return new SuccessMessage<List<SongList>>(null, songListService.likeStyle('%' + style + '%')).getMessage();
    }
    // 返回指定用户歌单

    @RequestMapping(value = "/songList/usersonglist",method = RequestMethod.GET)
    public Object getUserSongList(HttpServletRequest req){
        String userid = req.getParameter("userid").trim();
        Integer uid = Integer.parseInt(userid);
        return new SuccessMessage<List<SongList>>(null, songListService.likeUser(uid)).getMessage();
    }

    // 更新歌单信息
    @ResponseBody
    @RequestMapping(value = "/songList/update", method = RequestMethod.POST)
    public Object updateSongListMsg(HttpServletRequest req) {
        String id = req.getParameter("id").trim();
        String title = req.getParameter("title").trim();
        String introduction = req.getParameter("introduction").trim();
        String style = req.getParameter("style").trim();

        SongList songList = new SongList();
        songList.setId(Integer.parseInt(id));
        songList.setTitle(title);
        songList.setIntroduction(introduction);
        songList.setStyle(style);

        boolean res = songListService.updateSongListMsg(songList);
        if (res) {
            return new SuccessMessage<Null>("修改成功").getMessage();
        } else {
            return new ErrorMessage("修改失败").getMessage();
        }
    }

    // 更新歌单图片
    @ResponseBody
    @RequestMapping(value = "/songList/img/update", method = RequestMethod.POST)
    public Object updateSongListPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id") int id) {
        String fileName = avatorFile.getOriginalFilename();
        String imgPath = "/"+ ossConfiguration.getSongListPicPath() + fileName;

        ossService.uploadFile(avatorFile, ossConfiguration.getSongListPicPath()+fileName);

        SongList songList = new SongList();
        songList.setId(id);
        songList.setPic(imgPath);

        boolean res = songListService.updateSongListImg(songList);
        if (res) {
            return new SuccessMessage<String>("上传成功", imgPath).getMessage();
        } else {
            return new ErrorMessage("上传失败").getMessage();
        }
    }
}
